package com.yunhe.device.repository.base;

import com.yunhe.device.domain.base.DeviceType;
import com.yunhe.device.domain.device.Device;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName EquipmentTypeRepository
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/15 13:29
 * @Version 1.0
 **/
@Repository
public interface EquipmentTypeRepository extends Neo4jRepository<DeviceType,Long> {

    @Query("match(et:DeviceType{title:{title}}) where et.stationId is null return et")
    DeviceType findByTitle(@Param("title") String title);

    DeviceType findByName(String name);

    DeviceType findByTitleAndStationId(String title, Long stationId);

    @Query("match(et:DeviceType)-[:HAS_EQUIPATTR]->(ea:EquipmentAttributes) where ID(et)={id}  return count(ea) ")
    int getEquipAttrCountByEquipTypeId(@Param("id") Long id);

    @Query("match(et:DeviceType)-[:HAS_ANLOGTYPE]->(at:PointDataType) where ID(et)={id} return count(at) ")
    int getAnlogTypeCountByEquipTypeId(@Param("id") Long id);

    @Query("match(e:Device)-[:HAS_EQUIPTYPE]->(et:DeviceType) where ID(et)={id} and e.ownedPark IN {realStationIds} return count(e) ")
    int getEquipCountByEquipTypeId(@Param("id") Long id,@Param("realStationIds") List<Long> realStationIds);

    @Query("match(e:Device)-[:HAS_EQUIPTYPE]->(et:DeviceType) where ID(et)={id} return e")
    List<Device> getEquipByEquipTypeId(@Param("id") Long id);

    @Query("match(et:DeviceType)-[he:HAS_EQUIPATTR]->(ea:EquipmentAttributes) where ID(et)={id}  delete he ")
    void unbindEquipAttr(@Param("id") Long id);

    @Query("match(et:DeviceType)-[ha:HAS_ANLOGTYPE]->(at:PointDataType) where ID(et)={id} delete ha ")
    void unbindAnlogType(@Param("id") Long id);

    @Query("match(e:Device)-[he:HAS_EQUIPTYPE]->(et:DeviceType) where ID(et)={id} delete he, e,et")
    void deleteEquipByEquipTypeId(@Param("id") Long id);

    @Query("match(et:DeviceType),(ea:EquipmentAttributes) where ID(et)={id} and ID(ea) IN {equipAttrIds} create(et)-[:HAS_EQUIPATTR]->(ea)")
    void bindEquipAttr(@Param("id") Long id,@Param("equipAttrIds") List<Long> equipAttrIds);

    @Query("match(et:DeviceType),(at:PointDataType) where ID(et)={id} and ID(at) IN {anlogTypeIds} create(et)-[:HAS_ANLOGTYPE]->(at)")
    void bindAnlogType(@Param("id") Long id,@Param("anlogTypeIds") List<Long> anlogTypeIds);

    @Query("MATCH (et:DeviceType{activity:'1'}) WHERE 1=1 AND (et.stationId is null or et.stationId={stationId}) return et")
    List<DeviceType> findByStationId(@Param("stationId") Long stationId);

    @Query("match(e:Device)-[:HAS_EQUIPTYPE]->(et:DeviceType) where ID(e)={id} return et")
    DeviceType findByEuqipId(@Param("id") Long equipId);

    @Query("MATCH (et:DeviceType) where ID(et)={equipTypeId} set et.equipCount={equipCount} return et ")
    DeviceType addEquipCount(@Param("equipTypeId") Long equipTypeId,@Param("equipCount")  Long equipCount);

    @Query("match(ea:EquipmentAttributes)<-[heta:HAS_EQUIPTYPEATTR]-(e:Device)-[he:HAS_EQUIPTYPE]->(et:DeviceType) where ID(et)={id} delete heta")
    void unbindEquipAttrAndEquip(@Param("id") Long id);

    @Query("match(et:DeviceType),(at:PointDataType) where ID(et)={id} and ID(at) = {anlogTypeId} create(et)-[:HAS_ANLOGTYPE{stationId:{stationId}}]->(at)")
    void bindCustomizeAnlogType(@Param("id") Long id,@Param("anlogTypeId") Long anlogTypeIds,@Param("stationId") Long stationId);

    @Query("match(e:Device)-[heta:HAS_CUSTOMEQUIPTYPEATTR]->(ea:EquipmentAttributes) where ID(e)={id} delete heta,ea")
    void unbindCustEquipAttrAndEquip(@Param("id") Long id);

    @Query("match(e:Device)-[heta:HAS_EQUIPTYPEANLOGTYPE]->(at:PointDataType) where ID(e)={id} delete heta")
    void unbindEquipAndAnlogType(@Param("id") Long id);

    @Query("match(et:DeviceType{title:{title}}) where 1=1 AND (et.stationId is null or et.stationId={stationId}) AND et.title = {title}  return et")
    DeviceType findForImportDeviceByTitleAndStationId(@Param("title") String title,@Param("stationId") Long stationId);
}
